package org.zsl.controller.springmvc;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.zsl.model.ResponseMessage;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    public ResponseMessage fileUpload(MultipartFile photo, HttpSession session) throws IOException {

      String fileName=  photo.getOriginalFilename();
        System.out.println("1.fileName:"+fileName);

        String hznname= fileName.substring(fileName.lastIndexOf("."));

        System.out.println("2.hzName:"+hznname);


        fileName = "flower"+"."+hznname;

        /**
         * 获取当前项目所在的文件夹
         * */

        ServletContext context = session.getServletContext();

        String realPath= context.getRealPath("/");

        System.out.println("3.realpath:"+realPath);

        String uploadPath = context.getRealPath("upload");

        System.out.println("4.uploadPath:"+uploadPath);

        File  file = new File(uploadPath);

        if(!file.exists()){

            file.mkdir();
        }

        /**
         *  5. 全路径 ： 文件 + 文件名
         * */

        String finalPath = uploadPath + File.separator+fileName;

        System.out.println("5.finalPath:"+finalPath);

        /**
         *  如果文件上传不报异常 就定义为上传成功
         *  并修改 responseMessage中的相关信息
         * */
        photo.transferTo(new File(finalPath));

        return  new ResponseMessage();

    }


    @RequestMapping("/download/{filename}")
    public  void download(@PathVariable("filename") String filename,
                          HttpServletRequest request,
                          HttpServletResponse response) throws IOException {


       /**
        *  step1: 服务器端设置
        * */

       String realPath=  request.getSession().getServletContext().getRealPath("/upload");

        filename = filename + "." +"jpg";

        File file = new File(realPath,filename);

        FileInputStream fileInputStream = new FileInputStream(file);


        /**
         * step2: response设置
         * */

        response.setContentType("text/plain;charset=UTF-8");

        response.setHeader("content-disposition","attachment;fileName="+filename);

        /**
         *  step3 下载文件
         *     通过输入输出流进行文件复制
         * */
      ServletOutputStream os = response.getOutputStream();

      int len;
      byte[] bytes = new  byte[1024];
      while (true){

        len =fileInputStream.read(bytes);

        if(len==-1) break;
        os.write(bytes,0,len);
      }

      os.close();
      fileInputStream.close();



    }

}
